home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / NewsTicker.sit / NewsTicker / source code / Extractors / AppleExtractor.cp next >
Text File  |  1997-06-26  |  5KB  |  201 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    NewsTicker, my Hack for 1997
  4. #
  5. #    AppleExtractor.cp    -    Derived from HTMLExtractor, we get passed the tokens
  6. #                            and try to recognize headlines out of it.  We're
  7. #                            parsing "www.apple.com", Apple Computer's home page
  8. #
  9. ------------------------------------------------------------------------------*/
  10. #include "TickerGlobals.h"
  11. #include <string.h>
  12.  
  13. #include "AppleExtractor.h"
  14. #include "TickerCheckWebs.h"
  15. #include "HTMLExtractor.h"
  16.  
  17. // Check every hour
  18.  
  19. #define kAppleNewsPeriod 3600
  20.  
  21. // Globals for the Apple Extractor
  22.  
  23. unsigned long    gAppleNextTime = 0;
  24. Str31            gAppleLastMod = "\p";
  25.  
  26. #define kAppleAddress "www.apple.com"
  27.  
  28. class AppleExtractor: public HTMLExtractor
  29. {
  30.     protected:
  31.         enum    AppleParser    {    knaParsing,
  32.                                 //stories are <strong>head</strong><br>stuff</p>
  33.                                 knaHasStrong, knaHasNotStrong, knaHasBreak,
  34.                                 //images are <TD> <a><img></a>
  35.                                 knaHasLink
  36.                             };
  37.                                 
  38.         AppleParser    mfCurrentState;
  39.         Str255        mfTheURL;
  40.         Str255        mfTheSubject;
  41.         Boolean        mfInTD;
  42.      
  43.     public:
  44.                         AppleExtractor(sMyDataPtr theDataPtr);
  45.         virtual        ~AppleExtractor        (void);
  46.         
  47.         virtual void    HandleToken(char* string, short numchars, Boolean isCommand);
  48. };
  49.  
  50. //
  51. // We just parse the entries to find the element
  52. //
  53. AppleExtractor::AppleExtractor(sMyDataPtr theDataPtr)
  54.         :HTMLExtractor(kAppleAddress, 1001,  theDataPtr)
  55. {
  56.     unsigned long now;
  57.     
  58.     mfCurrentState = knaParsing;    //just waiting for our thing to come through
  59.     mfInTD = false;
  60.     
  61.     GetDateTime(&now);
  62.     gAppleNextTime = now + kAppleNewsPeriod;    //refresh apple every hour
  63. }
  64.  
  65. AppleExtractor::~AppleExtractor( void )    //remember the modification date
  66. {
  67.     PLstrcpy(gAppleLastMod, mfLastModified);
  68. }
  69.  
  70. void AppleExtractor::HandleToken(char* string, short numchars, Boolean isCommand)
  71. {
  72.     Str255    thestr;
  73.     
  74.     if (isCommand)
  75.     {
  76.         if (MyCompareStr(string, "<TD "))    //table delimiters mark the image links
  77.             mfInTD = true;
  78.         else if (MyCompareStr(string, "</TD "))
  79.         {
  80.             mfInTD = false;
  81.             mfCurrentState = knaParsing;
  82.         }
  83.         else switch (mfCurrentState)
  84.         {
  85.             case knaParsing:                        //from paragraph, we want <STRONG>
  86.                 if (MyCompareStr(string, "<STRONG>"))
  87.                 {
  88.                     mfCurrentState = knaHasStrong;
  89.                     mfTheSubject[0] = 0;
  90.                     mfTheURL[0] = 0;
  91.                 }
  92.                 else if (MyCompareStr(string, "<A ")&&mfInTD)
  93.                 {
  94.                     if (HTMLExtractor::ParseGoodURL(string+2, mfTheURL))
  95.                     {
  96.                         mfCurrentState = knaHasLink;
  97.                         mfTheSubject[0] = 0;
  98.                     }
  99.                     else mfCurrentState = knaParsing;
  100.                 }
  101.                 else mfCurrentState = knaParsing;
  102.                 break;
  103.                 
  104.             case knaHasStrong:                //for this, we only want </STRONG>
  105.                 if (MyCompareStr(string, "</STRONG>")&&(mfTheSubject[0]>0))
  106.                     mfCurrentState = knaHasNotStrong;
  107.                 else mfCurrentState = knaParsing;
  108.                 break;
  109.                 
  110.             case knaHasNotStrong:            //from here, we need a <BR>
  111.                 if (MyCompareStr(string, "<BR>"))
  112.                 {
  113.                     mfCurrentState = knaHasBreak;
  114.                 }
  115.                 else mfCurrentState = knaParsing;
  116.                 break;
  117.                 
  118.             case knaHasBreak:    //from here, remember <A> marks, abort on <P>, save on </P>
  119.                 if (MyCompareStr(string, "<A ")&&(mfTheURL[0]==0))
  120.                 {
  121.                     if (HTMLExtractor::ParseGoodURL(string+2, mfTheURL))
  122.                     {
  123.                         AddEntry(mfTheSubject,mfTheURL);
  124.                         mfCurrentState = knaParsing;
  125.                     }
  126.                 }
  127.                 else if (MyCompareStr(string, "<\P>"))    //OK, we got a success!
  128.                 {
  129.                     //Add the entry
  130.                     if (!mfTheURL[0])
  131.                         AddEntry(mfTheSubject, "\phttp://www.apple.com");
  132.                         else AddEntry(mfTheSubject, mfTheURL);    
  133.                     mfCurrentState = knaParsing;            
  134.                 }
  135.                 else if (MyCompareStr(string, "<P>"))    //Something wrong here
  136.                 {
  137.                     mfCurrentState = knaParsing;
  138.                 }
  139.                 break;
  140.             case knaHasLink:        //for this, we only want an img, if there's an alt text
  141.                 if (MyCompareStr(string, "<IMG "))
  142.                 {
  143.                     FindATag(string+4, (char*)&mfTheSubject[1], "ALT");
  144.                     mfTheSubject[0] = strlen( (char*)&mfTheSubject[1] );
  145.                     if ((mfTheSubject[0]>0)&&(!EqualString(mfTheSubject,"\pApple Sites Worldwide", false, false)))
  146.                     {
  147.                         AddEntry(mfTheSubject, mfTheURL);
  148.                     }
  149.                 }
  150.                 else if (MyCompareStr(string, "</A>"))
  151.                     mfCurrentState = knaParsing;
  152.                 break;
  153.         }
  154.     }
  155.     else
  156.     {
  157.         if (mfCurrentState==knaHasStrong)    //OK, get got a headline!
  158.         {
  159.             if (numchars>255)
  160.                 numchars = 255;
  161.             mfTheSubject[0] = numchars;
  162.             BlockMove(string, &mfTheSubject[1], numchars);    //remember it for later
  163.         }
  164.         else if ((mfCurrentState!=knaHasBreak)&&(mfCurrentState!=knaHasLink))
  165.             mfCurrentState = knaParsing;    //and wait for tne next headline
  166.     }
  167. }
  168.  
  169. void LoadAppleCom(sMyDataPtr gGlobalsPtr)
  170. {
  171.     AppleExtractor* theparser = new AppleExtractor(gGlobalsPtr);
  172.     
  173.     theparser->ReadEntries();
  174.     delete theparser;
  175.         
  176.     InitCursor();
  177. }
  178.  
  179. // See if we must reload.  Check the time to check, then get the last modified time
  180.  
  181. Boolean MustReloadAppleCom(sMyDataPtr    gGlobalsPtr)
  182. {
  183.     unsigned long now;
  184.     Str31    newModifiedTime;
  185.     
  186.     GetDateTime(&now);
  187.     
  188.     if (now<gAppleNextTime)    //time to check yet?
  189.         return false;
  190.     
  191.     GetModifiedDate( gGlobalsPtr, kAppleAddress, newModifiedTime);
  192.     if (EqualString(newModifiedTime, gAppleLastMod, false, false))
  193.     {
  194.         gAppleNextTime = now + kAppleNewsPeriod;
  195.         return false;
  196.     }
  197.     else
  198.     {
  199.         return true;
  200.     }
  201. }